package com.heyu.cgsd.ui.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.heyu.cgsd.R;

public class DialogPolicy extends AlertDialog {

    private Context mContext;
    private Button ivCancel;
    private OnPolicyListener policyLister;
    private TextView tvSuper;
    private Button ivAgreed;

    public DialogPolicy(Context context, OnPolicyListener lister) {
        super(context, R.style.dialog_loading);
        this.policyLister = lister;
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_policy);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        init();
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    private void init() {
        ivCancel = findViewById(R.id.iv_cancel);
        tvSuper = findViewById(R.id.tv_super);
        ivAgreed = findViewById(R.id.iv_agreed);

        final SpannableStringBuilder style = new SpannableStringBuilder();
        //设置文字
        style.append("1.在浏览使用时，我们会收集、使用设备信息用户体验优化。\n");
        style.append("2.我们可能会申请未知权限，用于推荐。\n");
        style.append("3.您可以查看完整版《用户协议》和《隐私政策》以便了解我们收集、使用、共享、存储信息的情况，以及会信息的保护措施。\n\n\n");
        style.append("您可以选择点击“同意”按钮接受我们的服务");
        //设置部分文字点击事件
        ClickableSpan clickablePolicy = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                if (policyLister != null) {
                    policyLister.onUser();
                }
            }

            @Override
            public void updateDrawState(TextPaint ds) {//重写该方法去掉下划线
                ds.setUnderlineText(false);
            }
        };

        ClickableSpan clickableUser = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                if (policyLister != null) {
                    policyLister.onPolicy();
                }
            }

            @Override
            public void updateDrawState(TextPaint ds) {//重写该方法去掉下划线
                ds.setUnderlineText(false);
            }
        };
        style.setSpan(clickablePolicy, 59, 65, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(clickableUser, 66, 72, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvSuper.setText(style);

        //设置部分文字颜色
        ForegroundColorSpan foregroundPolicy = new ForegroundColorSpan(Color.parseColor("#FED13C"));
        ForegroundColorSpan foregroundUser = new ForegroundColorSpan(Color.parseColor("#FED13C"));
        style.setSpan(foregroundPolicy, 59, 65, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(foregroundUser, 66, 72, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        //配置给TextView
        tvSuper.setMovementMethod(LinkMovementMethod.getInstance());
        tvSuper.setText(style);

        ivAgreed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (policyLister != null) {
                    policyLister.onAgreed();
                }
            }
        });
        ivCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (policyLister != null) {
                    policyLister.onCancel();
                }
            }
        });
    }

    public interface OnPolicyListener {
        void onPolicy();       //隐私政策

        void onUser();          //用户协议

        void onCancel();        //再看看

        void onAgreed();        //同意
    }
}